<template>
	<view>
		<div  class='content'>
			<u--form
				labelPosition="left"
				:model="model"
				ref="form1"
				labelWidth='24'
				:rules="rules"
			>
				<u-form-item
					prop="userInfo.phone"
					borderBottom
					leftIcon='phone'
					leftIconStyle='font-size:20px'
					ref="item1">
					<u--input
						v-model="model.userInfo.phone"
						border="none"  placeholder="请输入您的手机号"  maxlength="11">
					</u--input>
					<u-button
						slot="right"
						@tap="getCode"
						:text="tips"
						type="success"
						size="mini"
						:disabled="disabled1"
					></u-button>
				</u-form-item>
				<u-form-item
					prop="userInfo.verificationCode"
					borderBottom
					ref="item1">
					<u--input
						v-model="model.userInfo.verificationCode"
						border="none"  placeholder="请输入您收到的验证码">
					</u--input>
				</u-form-item>
				<u-form-item
					prop="userInfo.name"
					borderBottom
					leftIcon='lock'
					leftIconStyle='font-size:20px'
					ref="item1">
					<u--input
						v-model="model.userInfo.password"
						border="none"  placeholder="请设置您的登录密码" maxlength='6'>
					</u--input>
				</u-form-item>
			</u--form>
		</div>
		<div class='BTN'>
			<u-button @click='Reg' text="注册" shape='circle' color="linear-gradient(to right, rgb(46, 216, 196), rgb(54, 220, 170))"></u-button>
		</div>
		<u-code
			ref="uCode"
			@change="codeChange"
			seconds="20"
			@start="disabled1 = true"
			@end="disabled1 = false"
		></u-code>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model:{
					userInfo: {
						phone: null,
						password: '',
						verificationCode:'',
					},
				},
				rules: {
					'userInfo.phone': [
						{
							type: 'any',
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				},
				tips: '获取验证码',
				disabled1: false,
			};
		},

		methods: {
			Reg(){
				if(this.model.userInfo.phone && this.model.userInfo.password){
					this.$api.DLQregister({phone:this.model.userInfo.phone,password:this.model.userInfo.password,nickname:''}).then((res)=>{
						console.log(res)
						if(res.code == 500){
							uni.showToast({
								title:'账户名已被注册，请重新输入',
								icon:'error',
								duration:2000
							})
						}else{
							uni.navigateTo({
								url:'/pages/Center/login'
							})
						}
					})
				}else{
					uni.showToast({
						title:'不能为空',
						icon:"error"
					})
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码为：123456');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			
		},
	}
</script>

<style lang="scss" scoped>
	.content{
		border-radius: 26upx;
		padding: 30upx 40upx;
		margin: 36upx;
		box-shadow: 0 0 5px rgba(0,0,0,.1);
	}
	.BTN{
		margin: 36upx;
	}
</style>